package DbHelper;

import java.lang.reflect.Field;
import java.sql.*;
import java.util.ArrayList;

public class DbHelper {


    //提取步骤1+2：获得连接对象
    public Connection getCon() {
        Connection con = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            //2.获得连接对象
            String url = "jdbc:mysql://127.0.0.1:3306/boookmanger?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true";
            String username = "root";
            String password = "root";
            con = DriverManager.getConnection(url, username, password);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return con;
    }


    //（增删改）提取3+4+5，其中3以参数传入
    public int updateDate(String sql, Object... objs) {
        int count = 0;
        Connection con = null;//通过这个接口，应用程序可以与数据库进行交互，执行SQL语句，并处理结果。
        PreparedStatement ps = null;//预处理阶段
        try {
            //获得执行对象
            con = getCon();
            ps = con.prepareStatement(sql);
            //判断sql语句中有几个？
            for (int i = 0; i < objs.length; i++) {
                ps.setObject(i + 1, objs[i]);
            }
            //5.执行sql,获得结果
            count = ps.executeUpdate();//executeUpdate()执行更新方法
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //7.关闭
            closeAll(con, ps, null);
        }
        //6.忽略

        return count;

    }



    //[查询方法]，以参数传入
    public ArrayList query(String sql,Class c, Object... objs) {
        ArrayList list = new ArrayList<>();
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;//数据库查询结果集
        try {
            con = getCon();
            //4.
            ps = con.prepareStatement(sql);
            for (int i = 0; i < objs.length; i++) {
                ps.setObject(i + 1, objs[i]);
            }
            //5.执行sql,获得结果
            rs = ps.executeQuery();//处理查询sql命令
            //6.处理结果
            while(rs.next()){
                Object obj= c.newInstance();
                Field[] fs = c.getDeclaredFields();
                for (Field f:fs) {
                    String name = f.getName();
                    f.setAccessible(true);//暴力授权；
                    f.set(obj,rs.getObject(name));
                }
                list.add(obj);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } finally {
            closeAll(con, ps, rs);
        }
        //7.关闭
        return list;
    }
    //提取步骤7
    public void closeAll(Connection con, PreparedStatement ps, ResultSet rs) {
        try {
            if (rs != null) {
                rs.close();
            }
            if (ps != null) {
                ps.close();
            }
            if (con != null) {
                con.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
